package com.example.aoplibrary.aop

import android.util.Log
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut

@Aspect
class KotlinAdvice {
    companion object {
        @JvmStatic
        fun aspectOf(): KotlinAdvice {
            return KotlinAdvice()
        }
    }
//
//    @Pointcut("execution(* com.example.zxw.test.TestAop.testAopMethod())")
//    private fun pt() {
//    }
//
//
//    @Before("pt()")
//    fun printTimeAop() {
//        Log.e("AOP", "AOP切入java方法")
//    }

    @Pointcut("execution(* com.example.zxw.test.TestKotlinAop.testAop2())")
    private fun pt2() {
    }

    @Before("pt2()")
    fun printTimeAop2() {
        Log.e("AOP", "AOP切入Kotlin方法")
    }


    @Pointcut("@annotation(com.example.aoplibrary.aop.PrintMethodTime)")
//    @Pointcut("execution(@com.example.aoplibrary.aop.PrintMethodTime * *(..))")
    fun pt3(){}

//    @Before("pt3()")
//    fun printMethodTakeTime(){
//        Log.e("AOP", "打印方法耗时")
//    }

    @Around("pt3()")
    fun timeRecord(proceedingJoinPoint: ProceedingJoinPoint): Any? {
        try {
            val startTime = System.currentTimeMillis()
            val proceed = proceedingJoinPoint.proceed()
            val endTime = System.currentTimeMillis()
            val signature = proceedingJoinPoint.signature
            val declaringType = signature.declaringType
            val name = signature.name
            Log.e(
                "AOP",
                "耗时检测： " + declaringType + "  --  " + name + "  耗时： " + (endTime - startTime) + "ms"
            )
            return proceed
        }catch (e:Exception){
            e.printStackTrace()
        }

        return null
    }
}